//
//  DestinationViewController.m
//  AroundTheWorld
//
//  Created by IfelseGo on 16/5/14.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "DestinationViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "DataHandler.h"
#import "DestinationDataModel.h"
#import "countryModel.h"
#import "CountryTableViewCell.h"
#import "Hot_countryTableViewCell.h"
#import "CountryViewController.h"

//2
#import "CountryViewController.h"

static NSString *const countryReuse = @"countryReuse";
static NSString *const hot_countryReuse = @"hot_countryReuse";


@interface DestinationViewController ()

@property (nonatomic, strong) UITableView *myTabelView;
@property (nonatomic, strong) UIImageView *mapImageV;

@end

@implementation DestinationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = YES;

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, -5, WIDTH - 120, 25)];
    label.text = @"➢  旅 游 目 的 地 ";
    label.backgroundColor = [UIColor colorWithRed:58 / 255.0 green:160 / 255.0 blue:130 / 255.0 alpha:0.8];
    label.layer.masksToBounds = YES;
    label.layer.cornerRadius = 12;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
   
   
    [self getData];
    
    self.myTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.myTabelView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.myTabelView];
    
    //成为代理人
    self.myTabelView.delegate = self;
    self.myTabelView.dataSource = self;
    
    //注册
    UINib *nib = [UINib nibWithNibName:@"CountryTableViewCell" bundle:[NSBundle mainBundle]];
    [self.myTabelView registerNib:nib forCellReuseIdentifier:countryReuse];
    [self.myTabelView registerClass:[Hot_countryTableViewCell class] forCellReuseIdentifier:hot_countryReuse];
    
    //添加头视图
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 255)];
    headerView.backgroundColor = [UIColor yellowColor];
    self.myTabelView.tableHeaderView = headerView;
    
    [self addMap];
    
    //夜间模式
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0xCFCFCF);
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0x3AA082,0xCFCFCF);
    self.navigationItem.leftBarButtonItem= [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(nigthButtonAtcion:)];

}

//夜间模式判断
- (void)nigthButtonAtcion:(UIBarButtonItem *)item{
    if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNight]) {
        item.image = [[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        //        [self.isNightButton setImage:[UIImage imageNamed:@"btn_sun32"] forState:UIControlStateNormal];
        NSLog(@"白天");
        [self.dk_manager dawnComing];
    } else {
        NSLog(@"晚上");
        item.image = [[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        //        [self.isNightButton setImage:[UIImage imageNamed:@"btn_sun_night"] forState:UIControlStateNormal];
        [self.dk_manager nightFalling];
    }
}

- (void)buttonAtcion{
    CountryViewController *countryVC = [[CountryViewController alloc] init];
    [self presentViewController:countryVC animated:YES completion:^{
        
    }];
}

//data
-(void)getData{
    self.myDestinationArrayM = [NSMutableArray array];
    self.myHotCountyArray = [NSMutableArray array];
    self.myCnNameArray = [NSMutableArray array];
    DataHandler *handler1 = [[DataHandler alloc] init];
    [handler1 getDestinationBase: ^(id result) {
        self.myDestinationArrayM = [NSMutableArray arrayWithArray:result];
        DestinationDataModel *model = [self.myDestinationArrayM objectAtIndex:0];
        self.myCnNameArray = model.country;
        self.myHotCountyArray = model.hot_country;
        [self.myTabelView reloadData];
    } failedHandler:^(NSError *error) {
    }];
 }

//头视图加image
- (void) addMap{
    //button.tag = 100000 + number
    self.mapImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map"]];
    self.mapImageV.frame = CGRectMake(0, 0, WIDTH, 255);
    [self.myTabelView addSubview:self.mapImageV];
    
   self.button3 =  [self addButton:@"北美洲" Number:3];
   self.button3.frame = CGRectMake(30, 20, 100, 75);
    self.button2 =  [self addButton:@"欧洲" Number:2];
    self.button2.frame = CGRectMake(180, 15, 90, 65);
    self.button4 = [self addButton:@"南美洲" Number:4];
    self.button4.frame = CGRectMake(100, 130, 90, 65);
    self.button6 = [self addButton:@"非洲" Number:6];
    self.button6.frame = CGRectMake(185, 100, 90, 65);
    self.button7 = [self addButton:@"南极洲" Number:7];
    self.button7.frame = CGRectMake(175, 190, 90, 65);
    self.button1 = [self addButton:@"亚洲" Number:1];
    self.button1.frame = CGRectMake(320, 20, 90, 65);
    self.button5 = [self addButton:@"大洋洲" Number:5];
    self.button5.frame = CGRectMake(315, 130, 90, 65);
}

-(UIButton *)addButton:(NSString *)labelName Number:(NSInteger )number{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.backgroundColor = [UIColor colorWithRed:219 / 255.0 green:227 / 255.0 blue:233 / 255.0 alpha:0.9];
//    [button setTitle:labelName forState:UIControlStateNormal];
    if (number == 1) {
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"btn_county%ldc",number]] forState:UIControlStateNormal];
    } else {
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"btn_county%ld",number]] forState:UIControlStateNormal];
    }
    button.titleLabel.font = [UIFont systemFontOfSize: 12];
//    button.frame = CGRectMake(10 + 55 * (number - 1), 20, 80, 65);
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 10;
    [self.myTabelView addSubview:button];
    button.tag = 100000 + number;
    [button addTarget:self action:@selector(buttonAtcion:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}
- (void)buttonAtcion:(UIButton *)sender
{
    NSInteger baseTag = 100001;
    NSArray *titles = [NSArray arrayWithObjects:@"亚洲", @"欧洲", @"北美洲", @"南美洲", @"大洋洲", @"非洲", @"南极洲", nil];
    NSArray *array = [NSArray arrayWithObjects:self.button1, self.button2, self.button3, self.button4, self.button5, self.button6, self.button7, nil];
    
    //
    for (int i = 0; i < [array count]; i++) {
        UIButton *button = [array objectAtIndex:i];
        if (button != sender) {
            NSString *normalImageName = [NSString stringWithFormat:@"btn_county%ld", button.tag - baseTag + 1];
            [button setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];
        }
    }
    
    NSInteger index = sender.tag - baseTag;
    self.CNname = [titles objectAtIndex:index];
    DestinationDataModel *model = [self.myDestinationArrayM objectAtIndex:index];
    self.myCnNameArray = [NSMutableArray arrayWithArray:model.country];
    self.myHotCountyArray = [NSMutableArray arrayWithArray:model.hot_country];
    NSString *imageName = [NSString stringWithFormat:@"btn_county%ldc", index + 1];
    [sender setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
    [self.myTabelView reloadData];
}

- (void)buttonAtcion2:(id)sender
{
    self.myCnNameArray = [NSMutableArray array];
    self.myHotCountyArray = [NSMutableArray array];
    DestinationDataModel *model1 = [self.myDestinationArrayM objectAtIndex:0];
    DestinationDataModel *model2 = [self.myDestinationArrayM objectAtIndex:1];
    DestinationDataModel *model3 = [self.myDestinationArrayM objectAtIndex:2];
    DestinationDataModel *model4 = [self.myDestinationArrayM objectAtIndex:3];
    DestinationDataModel *model5 = [self.myDestinationArrayM objectAtIndex:4];
    DestinationDataModel *model6 = [self.myDestinationArrayM objectAtIndex:5];
    DestinationDataModel *model7 = [self.myDestinationArrayM objectAtIndex:6];
    UIButton *btn = (UIButton *)sender;
    if (btn.selected == NO) {
            switch (btn.tag) {
                case 100001:
                    self.myCnNameArray = [NSMutableArray arrayWithArray:model1.country];
                    self.myHotCountyArray = [NSMutableArray arrayWithArray:model1.hot_country];
                    self.CNname = @"亚洲";
                    [self.button1 setImage:[UIImage imageNamed:@"btn_county1c"] forState:UIControlStateNormal];
                    [self.myTabelView reloadData];
                    break;
                case 100002:
                    self.myCnNameArray = [NSMutableArray arrayWithArray:model2.country];
                    self.myHotCountyArray = [NSMutableArray arrayWithArray:model2.hot_country];
                    self.CNname = @"欧洲";
                    [self.button2 setImage:[UIImage imageNamed:@"btn_county2c"] forState:UIControlStateNormal];
                    [self.myTabelView reloadData];
                    break;
                case 100003:
                    self.myCnNameArray = [NSMutableArray arrayWithArray:model3.country];
                    self.myHotCountyArray = [NSMutableArray arrayWithArray:model3.hot_country];
                    self.CNname = @"北美洲";
                    [self.button3 setImage:[UIImage imageNamed:@"btn_county3c"] forState:UIControlStateNormal];
                    [self.myTabelView reloadData];
                    break;
                case 100004:
                    self.myCnNameArray = [NSMutableArray arrayWithArray:model4.country];
                    self.myHotCountyArray = [NSMutableArray arrayWithArray:model4.hot_country];
                    self.CNname = @"南美洲";
                    [self.button4 setImage:[UIImage imageNamed:@"btn_county4c"] forState:UIControlStateNormal];
                    [self.myTabelView reloadData];
                    break;
                case 100005:
                    self.myCnNameArray = [NSMutableArray arrayWithArray:model5.country];
                    self.myHotCountyArray = [NSMutableArray arrayWithArray:model5.hot_country];
                    self.CNname = @"大洋洲";
                    [self.button5 setImage:[UIImage imageNamed:@"btn_county5c"] forState:UIControlStateNormal];
                    [self.myTabelView reloadData];
                    break;
                case 100006:
                    self.myCnNameArray = [NSMutableArray arrayWithArray:model6.country];
                    self.myHotCountyArray = [NSMutableArray arrayWithArray:model6.hot_country];
                    self.CNname = @"非洲";
                    [self.button6 setImage:[UIImage imageNamed:@"btn_county6c"] forState:UIControlStateNormal];
                    [self.myTabelView reloadData];
                    break;
                case 100007:
                    self.myCnNameArray = [NSMutableArray arrayWithArray:model7.country];
                    self.myHotCountyArray = [NSMutableArray arrayWithArray:model7.hot_country];
                    self.CNname = @"南极洲";
                    [self.button7 setImage:[UIImage imageNamed:@"btn_county7c"] forState:UIControlStateNormal];
                    [self.myTabelView reloadData];
                    break;
                default:
                    break;
            }
        btn.selected = !btn.selected;
    }
    
}

//分区的个数
-(NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.myCnNameArray.count == 0) {
        return 1;
    }
    return 2;
}

//分区头高度
-(CGFloat )tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 50;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //背景颜色
    UIView *view = [UIView new];
//    view.backgroundColor = [UIColor whiteColor];
    view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0xCFCFCF);
    //标题
    UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, WIDTH -30 , 50)];
    [view addSubview:text];
    text.textColor = [UIColor grayColor];
    
    //文字定义
    if (section == 0) {
        if (self.CNname == nil) {
            text.text =@"亚洲热门目的地";
        }else
        {
            text.text = [NSString stringWithFormat:@"%@热门目的地", self.CNname];
        }
    }
    if (section == 1) {
        if (self.CNname == nil) {
            text.text =@"亚洲其他目的地";
        }else
        {
        text.text = [NSString stringWithFormat:@"%@其他目的地", self.CNname];
        UILabel *FGX = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 100, 0, 100 , 50)];
        FGX.text = @"拼音首字母排序";
        FGX.textColor = [UIColor grayColor];
        FGX.font = [UIFont systemFontOfSize:12];
        [view addSubview:FGX];
        }
    }
       return view;
}

//cell高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //分区1
    if (indexPath.section == 0) {
        
        if (self.myHotCountyArray.count == 1) {
            return (WIDTH - 50) * 230 / 300 + 50;
        }
        return (WIDTH - 50) * 230 / 300  * 4 + 20 * 2 + 10 * 3;
    }
    //分区2
    return 50;
}


//cell数
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return self.myCnNameArray.count;
}

//建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        Hot_countryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hot_countryReuse];
        cell.smallArray = self.myHotCountyArray;
        //B--->A 点击国家Id
        cell.myBlock = ^(NSString *countryid){
            CountryViewController *countryVC = [[CountryViewController alloc] init];
            countryVC.countyID = countryid;
            [self presentViewController:countryVC animated:YES completion:^{
                [self.myTabelView reloadData];
            }];
        };
        return cell;
    }
        CountryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:countryReuse];
        countryModel *model = [self.myCnNameArray objectAtIndex:indexPath.row];
        cell.myModel = model;
        return cell;
}
//触发方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    CountryViewController *countryVC = [[CountryViewController alloc] init];
    countryModel *model = [self.myCnNameArray objectAtIndex:indexPath.row];
    countryVC.countyID = model.countryID;
    [self presentViewController:countryVC animated:YES completion:^{
        [self.myTabelView reloadData];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
